package leetcode;

import java.util.Arrays;

public class Leetcode106 {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if(postorder.length==0){
            return null;
        }
        int rootVal = postorder[postorder.length-1];
        int rootIndex = inorder[0];
        for (int i = 0; i < inorder.length; i++) {
            if(rootVal==inorder[i]){
                rootIndex = i;
                break;
            }
        }
        TreeNode root = new TreeNode(rootVal);
        root.left = buildTree(Arrays.copyOfRange(inorder,0,rootIndex),Arrays.copyOfRange(postorder,0,rootIndex));
        root.right = buildTree(Arrays.copyOfRange(inorder,rootIndex+1,inorder.length),Arrays.copyOfRange(postorder,rootIndex,postorder.length-1));
        return root;
    }
}
